class Solution:
    def calculate(self, s: str) -> int:
        can = set([i + j for i in '+-*/' for j in '+-$'])
        can = can.union({'**', '/*', '*/', '//'})
        nums, ops = [], []
        ops_len, ans = 0, 0
        s += '$'
        for si in s:
            if si in '0123456789':
                ans = ans * 10 + ord(si) - ord('0')
            elif si in '+-*/$':
                while ops and ops[ops_len - 1] + si in can:
                    a, ss = nums.pop(), ops.pop()
                    ans = a + ans if ss == '+' else (a - ans if ss == '-' else (a * ans if ss == '*' else a//ans))
                    ops_len -= 1
                else:
                    nums.append(ans)
                    ans = 0
                    ops.append(si)
                    ops_len += 1
        if nums:
            ans = nums[0]
        return ans


solve = Solution()

questions = [
    ("3+2*2",),
    ("1",),
    ("1+1+1",),
    ("2*3*4",),
    (" 2* 3*4 ",),
    ("14/3*2",),
]

for qi, q in enumerate(questions, 1):
    print(f'#{qi}:', q)
    s, = q
    print('ans:', solve.calculate(s))
